package com.ywq.wechatordersys.controller;


import com.ywq.wechatordersys.VO.Result;
import com.ywq.wechatordersys.converter.OrderForm2OrderDTOConverter;
import com.ywq.wechatordersys.dto.OrderDTO;
import com.ywq.wechatordersys.exception.MyException;
import com.ywq.wechatordersys.form.OrderForm;
import com.ywq.wechatordersys.service.OrderService;
import com.ywq.wechatordersys.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/buyer/order")
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;

    //创建订单
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result<OrderDTO> createOrder(@Validated OrderForm orderForm, BindingResult bindingResult) {
        //这个地方我们创建一个新的包 使用新的对象接受 进行表单验证

        if (bindingResult.hasErrors()) {
            //如果有错误 ,就
            throw new MyException(100, bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.converter(orderForm);

        if (orderDTO == null) {
            throw new MyException(100, "param is null");
        }
        if (orderDTO.getOrderDetailList().isEmpty()) {
            throw new MyException(100, "购物车是空的");

        }

        OrderDTO orderDTO1 = orderService.create(orderDTO);
        return ResultUtils.success(orderDTO1);
    }


    //订单列表
    @GetMapping(value = "/list")
    public Result<List<OrderDTO>> list(@RequestParam(value = "openid", required = true) String openid,
                                       @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo,
                                       @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ) {
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        List<OrderDTO> list = orderService.findList(openid, pageRequest);
        return ResultUtils.success(list);
    }


    //取消订单
    @PostMapping(value = "/cancel")
    public Result<Boolean> cancel(@RequestParam(value = "orderId", required = true) String orderId
    ) {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setOrderId(orderId);
        OrderDTO dto = orderService.cancel(orderDTO);
        if (dto == null) {
            throw new MyException(100, "订单取消异常");
        } else {
            return ResultUtils.success(true);
        }

    }


    //订单详情
    @GetMapping(value = "/details")
    public Result<OrderDTO> details(@RequestParam(value = "orderId", required = true) String orderId
    ) {

        OrderDTO dto = orderService.findOne(orderId);
        //订单详情
        if (dto == null) {
            throw new MyException(100, "订单查询异常");
        } else {
            return ResultUtils.success(dto);
        }

    }

}
